package com.lvhx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

// http://localhost:9999/lines

@Api(tags = "读取文件")
@RestController
public class ReadFileController {

    /**
     * 逐行读取文件内容
     * 适合读取大文件，不会把文件一次性加载到内存
     */
    @ApiOperation("逐行读取")
    @GetMapping("/lines")
    public void lines() {
        Path filePath = Paths.get("D:\\readfile.txt");

        try (Stream<String> lines = Files.lines(filePath)) {
            lines.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 读取所有文件内容
     * 会把文件一次性加载到内存
     */
    @ApiOperation("一次性读取")
    @GetMapping("/readAllLines")
    public void readAllLines() {
        String filePath = "D:\\readfile.txt";
        try {
            List<String> lines = Files.readAllLines(Paths.get(filePath));
            for (String line : lines) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @ApiOperation("逐行读取-bufferedReader")
    @GetMapping("/bufferedReader")
    public void bufferedReader() {
        String filePath = "D:\\readfile.txt";
        try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("用Files创建文件")
    @GetMapping("/createFileByFiles")
    public void createFileByFiles() {
        // 定义文件路径
        Path filePath = Paths.get("D:\\writefile.txt");

        // 要写入文件的内容
        List<String> lines = Arrays.asList("第一行", "第二行", "第三行");

        try {
            // 创建文件并写入内容
            Files.write(filePath, lines, StandardCharsets.UTF_8);
            System.out.println("文件创建并写入成功！");
        } catch (IOException e) {
            System.err.println("文件操作失败: " + e.getMessage());
        }
    }


    @ApiOperation("用BufferedWriter创建文件")
    @GetMapping("/createFileByBufferedWriter")
    public void createFileByBufferedWriter() {
        // 定义文件路径
        String filePath = "D:\\writefile.txt";

        // 要写入文件的内容
        List<String> lines = Arrays.asList("第一行", "第二行", "第三行");

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String line : lines) {
                writer.write(line);
                writer.newLine(); // 写入换行符
            }
            System.out.println("文件创建并写入成功！");
        } catch (IOException e) {
            System.err.println("文件操作失败: " + e.getMessage());
        }
    }
}
